/*
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements.  See the NOTICE file
distributed with this work for additional information
regarding copyright ownership.  The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License.  You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied.  See the License for the
specific language governing permissions and limitations
under the License.
 */
package td.pt.widget;

import td.client.ClientState;
import td.domain.Category;
import td.domain.DemoStore;
import td.domain.Topic;
import td.pt.event.CreateTopicEvent;
import td.pt.event.EditEvent;
import td.pt.event.CreateTopicEvent.CreateTopicHandler;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.web.bindery.event.shared.EventBus;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.TextArea;
import com.google.gwt.user.client.ui.TextBox;
import com.google.inject.Inject;
import com.gwtplatform.mvp.client.PopupView;
import com.gwtplatform.mvp.client.PresenterWidget;
import com.gwtplatform.mvp.client.proxy.RevealRootPopupContentEvent;

/**
 * Logic behind the pop-up presenting dialog for creation of new Topic.
 * 
 * @author Artem Shamsutdinov
 * 
 */
public class NewTopicPopupPresenter extends
		PresenterWidget<NewTopicPopupPresenter.MyView> implements
		CreateTopicHandler {

	public interface MyView extends PopupView {
		Label getInitialCategory();

		TextBox getName();

		TextArea getDescription();

		Button getCreateTopic();

		Button getCancel();
	}

	Category initialCategory;
	
	@Inject
	ClientState state;

	@Inject
	public NewTopicPopupPresenter(final EventBus eventBus, final MyView view) {
		super(eventBus, view);
	}

	@Override
	protected void onBind() {
		super.onBind();
		registerHandler(getEventBus().addHandler(CreateTopicEvent.TYPE, this));
		getView().getCreateTopic().addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {
				String name = getView().getName().getValue();
				String description = getView().getDescription().getValue();
				if (validateTopic(name, description)) {
					Topic topic = DemoStore.db().addTopic(initialCategory,
							name, description);
					state.getThinker().getDraft().setTopic(topic);
					EditEvent editEvent = new EditEvent(topic);
					getEventBus().fireEvent(editEvent);
					getView().hide();
				}
			}
		});
		getView().getCancel().addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {
				getView().hide();
			}
		});
	}

	boolean validateTopic(String name, String description) {
		StringBuilder errorMessages = new StringBuilder();
		if (name == null || name.trim().isEmpty()) {
			errorMessages.append("Name cannot be empty\n");
		}
		if (description == null || name.trim().isEmpty()) {
			errorMessages.append("Description cannot be empty\n");
		}
		if (errorMessages.length() > 0) {
			Window.alert(errorMessages.toString());
		}
		return errorMessages.length() == 0;
	}

	@Override
	protected void onReveal() {
		super.onReveal();
	}

	@Override
	public void onCreateTopic(CreateTopicEvent event) {
		initialCategory = event.getInitialCategory();
		getView().getInitialCategory().setText(initialCategory.getName());
		RevealRootPopupContentEvent.fire(event.getSource(),
				NewTopicPopupPresenter.this);
	}
}
